﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Business;

namespace BaseWeb.SystemSetting.Menu
{
    public partial class MenuList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }
        }

        private void BindTree()
        {
            this.tv.Nodes.Clear();
            TreeNode node = new TreeNode() { Text = "根节点", Value = "0" };
            this.tv.Nodes.Add(node);
            BindChild(node);
            this.tv.ExpandAll();
        }

        private void BindChild(TreeNode parentNode)
        {
            List<li_Menu> arryMenu = CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == parentNode.Value).OrderBy(c => c.OrderIndex).ToList();
            foreach (var enMenu in arryMenu)
            {
                TreeNode node = new TreeNode()
                {
                    Text = enMenu.Name,
                    Value = enMenu.KeyId
                };
                parentNode.ChildNodes.Add(node);
                BindChild(node);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hid.Value))
            {
                if (string.IsNullOrEmpty(this.txtKeyId.Text))
                {
                    txtKeyId.Text = Guid.NewGuid().ToString();
                }
                try
                {
                    li_Menu enMenu = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(this.txtKeyId.Text);
                    if (enMenu == null)
                    {
                        enMenu = new li_Menu();
                        enMenu.KeyId = this.txtKeyId.Text;
                        SetEntityInfo(enMenu);
                        CommonSystem.Instance.Service.AddEntity<li_Menu>(enMenu);
                    }
                    else
                    {
                        SetEntityInfo(enMenu);
                        CommonSystem.Instance.Service.UpdateEntity<li_Menu>(enMenu);
                    }
                    BindTree();
                    labMsg.Text = "保存成功";
                }
                catch (Exception ex)
                {
                    labMsg.Text = ex.Message;
                }
            }
        }


        private void SetEntityInfo(li_Menu enMenu)
        {
            enMenu.Name = this.txtName.Text;
            enMenu.OrderIndex = Convert.ToDouble(this.txtOrderIndex.Text);
            enMenu.Toop = this.txtToop.Text;
            enMenu.Url = this.txtUrl.Text;
            enMenu.ParentKeyId = this.txtParentKeyId.Text;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtKeyId.Text))
            {
                CommonSystem.Instance.Service.DeleteEntity<li_Menu>(this.txtKeyId.Text);
                this.txtKeyId.Text = "";
                this.txtName.Text = "";
                this.txtOrderIndex.Text = "";
                this.txtToop.Text = "";
                this.txtUrl.Text = "";
                this.hid.Value = "";
                BindTree();
                this.txtParentKeyId.Text = this.hid.Value;
                labMsg.Text = "删除成功";
            }
        }

        protected void tv_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.tv.SelectedNode != null)
            {
                if (tv.SelectedNode.Value == "0")
                {
                    this.txtKeyId.Text = tv.SelectedNode.Value;
                    this.txtName.Text = tv.SelectedNode.Text;
                    this.btnSave.Enabled = false;
                }
                else
                {
                    li_Menu enMenu = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(tv.SelectedNode.Value);
                    if (enMenu != null)
                    {
                        this.hid.Value = enMenu.ParentKeyId;
                        BindInfo(enMenu.KeyId);
                    }
                    this.btnSave.Enabled = true;
                }
                this.txtParentKeyId.Text = this.hid.Value;
                labMsg.Text = "";
            }

        }

        private void BindInfo(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var enMenu = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(id);
                if (enMenu != null)
                {
                    this.txtKeyId.Text = enMenu.KeyId;
                    this.txtName.Text = enMenu.Name;
                    this.txtOrderIndex.Text = enMenu.OrderIndex.ToString();
                    this.txtToop.Text = enMenu.Toop;
                    this.txtUrl.Text = enMenu.Url;
                }
                else
                {
                    this.txtKeyId.Text = "";
                    this.txtName.Text = "";
                    this.txtOrderIndex.Text = "";
                    this.txtToop.Text = "";
                    this.txtUrl.Text = "";
                }
            }
        }

        protected void btnAddSub_Click(object sender, EventArgs e)
        {
            if (this.txtKeyId.Text == "0")
            {
                this.hid.Value = "0";
            }
            else
            {
                var enMenu = CommonSystem.Instance.Service.GetInfoByKeyId<li_Menu>(this.txtKeyId.Text);
                if (enMenu != null)
                {
                    this.hid.Value = enMenu.KeyId;
                }
            }
            this.txtParentKeyId.Text = this.hid.Value;
            this.txtKeyId.Text = "";
            this.txtName.Text = "";
            var list = CommonSystem.Instance.Service.GetListByWhere<li_Menu>(c => c.ParentKeyId == this.hid.Value).ToList();
            if (list.Count > 0)
            {
                this.txtOrderIndex.Text = (Convert.ToDouble(list[list.Count - 1].OrderIndex) + 1).ToString();
            }
            else
            {
                this.txtOrderIndex.Text = "1";
            }
            this.txtToop.Text = "";
            this.txtUrl.Text = "";
            labMsg.Text = "";
            this.btnSave.Enabled = true;
        }


    }
}
